package com.fire.control.system.support.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
public class QrcodeUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(QrcodeUtil.class);
	
    public static void main(String[] args) {
    	//CCRDFile.createDir("E:\\lora-files\\qrcode\\");
        //createQRCode();
        //readQRCode();
    }
    
    private static void readQRCode() {
        try {
            MultiFormatReader formatReader = new MultiFormatReader();
            File file = new File("e:/image.png");
            BufferedImage image = null;
            image = ImageIO.read(file);
            HashMap hashMap = new HashMap();
            hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            Result result = formatReader.decode(binaryBitmap, hashMap);
            System.out.println("解析结果："+result.toString());
            System.out.println("二维码格式类型："+result.getBarcodeFormat());
            System.out.println("二维码文本："+result.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void createQRCodeApp(){
    	CCRDFile.createDir(StaticValue.file_upload_path);
    	String andorid_qrcode_url = createQRCode(StaticValue.download_android_url, "android", 300, 300, "png");
    	logger.info("--------->android二维码地址:"+andorid_qrcode_url);
    	String ios_qrcode_url = createQRCode(StaticValue.download_ios_url, "ios", 300, 300, "png");
    	logger.info("--------->ios二维码地址:"+ios_qrcode_url);
    }
    
    private static String createQRCode(String content,String qrcodeName,Integer width,Integer height,String format){
    	if(width==null)width = 300;
    	if(height==null)height = 300;
    	if(StringUtils.isEmpty(format))format = "png";
        //定义二维码的参数
        Map<EncodeHintType,Object> hashMap = new HashMap<EncodeHintType,Object>();
        hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hashMap.put(EncodeHintType.MARGIN, 5);
        Path file = null;
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hashMap);
            file = new File(StaticValue.file_upload_path+qrcodeName+"."+format).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return StaticValue.file_server_domain+"/"+file.getFileName();
    }
 
    /*private static void createQRCode() {
        int width = 300;
        int height = 300;
        String format = "png";
        String content = "http://www.baidu.com";
        //定义二维码的参数
        Map<EncodeHintType,Object> hashMap = new HashMap<EncodeHintType,Object>();
        hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hashMap.put(EncodeHintType.MARGIN, 5);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hashMap);
            //Path file = new File("src/main/resources/static/qrcode/app_qrcode.png").toPath();
            Path file = new File("d:/img/img3.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}